﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;
using LUStationery.Model;
using LUStationery.DAO;
using LUStationery.Controller;

namespace LUStationery.Controller
{

    public class UsersControl
    {
        public User getUserByUserID(string userID)
        {
            UserDAO uDAO = new UserDAO();
            IDataReader idr = uDAO.getUserByUserID(userID);
            return UsersControl.convertIdrToUser(idr);
        }

        public User getUserByUserName(string userName)
        {
            UserDAO uDAO = new UserDAO();
            IDataReader idr = uDAO.getUserByUserName(userName);
            return UsersControl.convertIdrToUser(idr);
        }

        public User getUserByEmail(string email)
        {
            UserDAO uDAO = new UserDAO();
            IDataReader idr = uDAO.getUserByEmail(email);
            return UsersControl.convertIdrToUser(idr);
        }


        public UserList getUsersByDeptCode(string deptCode)
        {
            UserDAO uDAO = new UserDAO();
            IDataReader idr = uDAO.getUsersByDepartment(deptCode);
            return UsersControl.convertIdrToUserList(idr);
        }

        public UserList getUsersByDepartment(string deptName)
        {
            return getUsersByDeptName(deptName);
        }

        public UserList getUsersByDeptName(string deptName)
        {
            UserDAO uDAO = new UserDAO();
            IDataReader idr = uDAO.getUsersByDeptName(deptName);
            return UsersControl.convertIdrToUserList(idr);

        }

        public UserList getUsersByDepartmentAndPosition(string deptCode, string position)
        {
            UserDAO uDAO = new UserDAO();
            IDataReader idr = uDAO.getUsersByDepartmentAndPosition(deptCode, position);
            return UsersControl.convertIdrToUserList(idr);
        }



        protected static User convertIdrToUser(IDataReader idr)
        {
            User user = new User();
            while (idr.Read())
            {
                user.StaffID = Convert.ToString(idr["staff_id"]);
                user.UserName = Convert.ToString(idr["user_name"]);
                user.DepartmentCode = Convert.ToString(idr["department_code"]);
                user.DepartmentName = Convert.ToString(idr["department_name"]);
                user.Position = Convert.ToString(idr["position"]);
                user.Role = Convert.ToString(idr["role"]);
                user.Email = Convert.ToString(idr["email"]);
                user.Password = Convert.ToString(idr["password"]);
                break;
            }
            return user;
        }

        protected static UserList convertIdrToUserList(IDataReader idr)
        {
            UserList uList = new UserList();
            while (idr.Read())
            {
                User user = new User();
                user.StaffID = Convert.ToString(idr["staff_id"]);
                user.UserName = Convert.ToString(idr["user_name"]);
                user.DepartmentCode = Convert.ToString(idr["department_code"]);
                user.DepartmentName = Convert.ToString(idr["department_name"]);
                user.Position = Convert.ToString(idr["position"]);

                user.Role = Convert.ToString(idr["role"]);
                uList.Add(user);
            }
            return uList;
        }
    }
}